function DataHolder() {
    this.data = {}; //key = value
    this.updates = {}; //key = status
}

DataHolder.create = function() {
    gDBWorld.insert({_id: 'data_holder'}, function(err, result){});
}

DataHolder.prototype = {
    init: function(callback) {
        gDBWorld.findOne({_id : 'data_holder'}, {}, function(err, doc) {
            if (doc) {
                this.data = doc;
                delete this.data['_id'];
                callback && callback(true);
            } else {
                callback && callback(false);
            }
        }.bind(this));
    },

    get: function(key) {
        return this.data[key];
    },

    set: function(key, value) {
        this.data[key] = value;
        this.update(key);
        this.save();
    },

    getNumber: function(key) {
        return this.data[key]||0;
    },

    addNumber: function(key, value) {
        addAttrValue(this.data, key, value);
        this.update(key);
        this.save();
    },

    update: function(key) {
        this.updates[key] = 1;
    },

    save: function(force, callback) {
        var count = Object.keys(this.updates).length;
        if (count <= 0) {
            callback && callback(true);
            return;
        }

        var doc = {};
        for (var key in this.updates) {
            doc[key] = this.data[key];
        }
        this.updates = {};

        gDBWorld.update({_id: 'data_holder'}, {$set: doc}, function(err, result) {
            if (err) {
                ERROR(util.format('SAVE DATA_HOLDER %j %j', doc, err));
                callback && callback(false);
            }else {
                LOG(util.format('SAVE DATA_HOLDER %j', doc));
                callback && callback(true);
            }
        }.bind(this));
    },


    check: function() {
        this.save();
    },
}

exports.DataHolder = DataHolder;
